<?php

declare(strict_types=1);

namespace app\common\model;

use addons\Target\facade\Target;
use addons\Pay\facade\Pay;

class RemitOrder extends Base
{
    protected $name = 'remit_order';
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,mobile');
    }

    public function getAccountAttr($value)
    {
        return get_pay_way($value);
    }
    /**
     * 微信充值
     */
    public static function wechatAdd($param, $user, $system): array
    {
        try {
            if (!$system['remit_open']) {
                return ['code' => -1, 'msg' => '充值通道已关闭'];
            }
            if ($param['amount'] < $system['remit_min']) {
                return ['code' => -1, 'msg' => '最低充值：' . $system['remit_min']];
            }
            if ($system['remit_bs']) {
                if ($param['amount'] % $system['remit_bs'] != 0) {
                    return ['code' => -1, 'msg' => '充值倍数：' . $system['remit_bs']];
                }
            }
            $info = parent::create([
                'sn'              => 'R' . date('YmdHis') . $user->id . rand(100000, 999999),
                'uid'             => $user->id,
                'money'           => $param['amount'],
            ]);
            $return_url = 'http://' . $_SERVER['HTTP_HOST'] . '/index/user/index.html';
            $notifyUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/index/callback/wechatRemitCallback';
            $options = Pay::setDriver('WeChat')
                ->setReturnUrl($return_url)
                ->basic('充值', $info->sn, $info->money, $notifyUrl)
                ->create('MWEB');
            return ['code' => 1, 'msg' => '支付中', 'url' => $options];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
    /**
     * 充值回调
     */ public static function operations($info, $user, $account): array
    {
        $sys = Config::lists();
        parent::startTrans();
        try {
            $info->pay_way = $account;
            $info->pay_state = 2;
            $info->pay_time = time();
            $info->save();
            if ($account == 'wechat') {
                parent::create([
                    'uid'     => $user->id,
                    'account' => 'bonus',
                    'amount'  => $info['money'],
                    'money'  => $info['money'],
                    'images'  => '',
                    'pay_way' => '微信支付',
                    'status' => 2,
                    'confirm_time' => time(),
                ]);
                Target::Account()->basics($info['uid'], 'bonus', $info['money'], '充值', '+')
                    ->createNormal();
            }
            //统计收入，则需使用
            parent::commit();
            return ['code' => 1, 'msg' => '充值成功', 'url' => url('user/index')->build()];
        } catch (\Exception $e) {
            parent::rollback();
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
